package cn.netuo.bumbersoll.helper;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import cn.netuo.bumbersoll.App;
import cn.netuo.bumbersoll.BizConstant;
import cn.netuo.bumbersoll.entity.MqttFromC;
import cn.netuo.bumbersoll.entity.MqttFromS;
import cn.netuo.bumbersoll.mqtt.MQTTService;
import cn.netuo.bumbersoll.util.BsUtil;
import lib.grasp.util.NumberUtil;

/**
 * 客户端给服务器的消息
 */
public class MqttFromCHelper {

    /** 发送业务心跳 */
    public static void sendHeartBeat(Context ctx, boolean isPlaying, List<String> playList){
        MqttFromC mqttFromC     = new MqttFromC();
        mqttFromC.cmd           = BizConstant.CMD_TYPE_HEARTBEAT;
        mqttFromC.requestId     = "";
        mqttFromC.code          = "200";

        mqttFromC.deviceId      = BsUtil.getMqttTopic(ctx);                 // 服务器接收的是MQTT订阅主题
        mqttFromC.battery       = App.getApp().getBatteryReceiver().getBatteryLeftRate(ctx);
        mqttFromC.isCharging    = App.getApp().getBatteryReceiver().isBatteryCharging(ctx);
        mqttFromC.lat           = String.valueOf(BizConstant.LOCATE_LAT);
        mqttFromC.lng           = String.valueOf(BizConstant.LOCATE_LNG);
        mqttFromC.versionName   = App.getApp().getAppVersionName();
        mqttFromC.versionCode   = App.getApp().getAppVersionCode();
        mqttFromC.patchVersion  = BizConstant.SYS_PATCH_VERSION;
        mqttFromC.timeStamp     = String.valueOf(System.currentTimeMillis());

        mqttFromC.vol           = NumberUtil.getProgress(BsUtil.getMediaVol(ctx), 15);
        mqttFromC.isPlaying     = isPlaying;
        mqttFromC.playList      = playList;
        String res = mqttFromC.toJson();
        MQTTService.publish(res);
    }

    /** 发送业务回执 */
    public static void doSendReceipt(Context ctx, MqttFromS mqttFromS, boolean isPlaying, List<String> playList){
        MqttFromC mqttFromC     = new MqttFromC();
        mqttFromC.cmd           = mqttFromS.cmd;
        mqttFromC.requestId     = mqttFromS.requestId;
        mqttFromC.code          = "200";

        mqttFromC.deviceId      = BsUtil.getMqttTopic(ctx);                    // 服务器接收的是MQTT订阅主题
        mqttFromC.battery       = App.getApp().getBatteryReceiver().getBatteryLeftRate(ctx);
        mqttFromC.isCharging    = App.getApp().getBatteryReceiver().isBatteryCharging(ctx);
        mqttFromC.lat           = String.valueOf(BizConstant.LOCATE_LAT);
        mqttFromC.lng           = String.valueOf(BizConstant.LOCATE_LNG);
        mqttFromC.versionName   = App.getApp().getAppVersionName();
        mqttFromC.versionCode   = App.getApp().getAppVersionCode();
        mqttFromC.patchVersion  = BizConstant.SYS_PATCH_VERSION;
        mqttFromC.timeStamp     = String.valueOf(System.currentTimeMillis());

        mqttFromC.vol           = NumberUtil.getProgress(BsUtil.getMediaVol(ctx), 15);
        mqttFromC.isPlaying     = isPlaying;
        mqttFromC.playList      = playList;
        String res = mqttFromC.toJson();
        MQTTService.publish(res);
    }

    /** 定制业务遗嘱 */
    public static String getLastWill(Context ctx){
        MqttFromC mqttFromC     = new MqttFromC();
        mqttFromC.cmd           = "lastWill";
        mqttFromC.requestId     = UUID.randomUUID().toString();
        mqttFromC.code          = "400";

        mqttFromC.deviceId      = BsUtil.getMqttTopic(ctx);                // 服务器接收的是MQTT订阅主题
        mqttFromC.battery       = 0;
        mqttFromC.isCharging    = false;
        mqttFromC.lat           = String.valueOf(BizConstant.LOCATE_LAT);
        mqttFromC.lng           = String.valueOf(BizConstant.LOCATE_LNG);
        mqttFromC.versionName   = App.getApp().getAppVersionName();
        mqttFromC.versionCode   = App.getApp().getAppVersionCode();
        mqttFromC.patchVersion  = BizConstant.SYS_PATCH_VERSION;
        mqttFromC.timeStamp     = String.valueOf(System.currentTimeMillis());

        mqttFromC.vol           = -1;
        mqttFromC.isPlaying     = false;
        mqttFromC.playList      = new ArrayList<>();
        return mqttFromC.toJson();
    }
}
